package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.CampusMapper;
import com.mk.admin.mapper.DepartmentMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.CampusDto;
import com.mk.admin.model.dto.DepartmentDto;
import com.mk.admin.model.dto.HttpDepartmentDto;
import com.mk.admin.model.entity.CampusEntity;
import com.mk.admin.model.entity.DepartmentEntity;
import com.mk.admin.model.query.DepartmentQuery;
import com.mk.admin.model.vo.ClearVo;
import com.mk.admin.model.vo.DepartmentNumVo;
import com.mk.admin.model.vo.DepartmentVo;
import com.mk.admin.service.DepartmentService;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CampusMapper campusMapper;

    @Override
    public ResultDto select(DepartmentQuery query) {
        DepartmentEntity entity = JSONCopy.copy(query, DepartmentEntity.class);
        return ResultDto.data(departmentMapper.count(entity), departmentMapper.select(entity));
    }

    @Override
    public ResultDto clear(ClearVo vo) {
        //        清空院区表数据
        campusMapper.clear(vo.tables);
        return ResultDto.data("清空数据成功");
    }

    @Override
    public ResultDto num(DepartmentNumVo vo) {
        DepartmentEntity departmentEntity = new DepartmentEntity();
        departmentEntity.setDeptType("01");
        JSONObject jsonObject = HttpTool.postJson("opt_0002", departmentEntity);
        Object result = jsonObject.get("result");
        List<DepartmentDto> dtoList = new ArrayList<>();
        List<JSONObject> objects = JSONArray.parseArray(result.toString(), JSONObject.class);
        objects.forEach(item -> {
            DepartmentDto dto = new DepartmentDto();
            dto.setName(item.get("deptName") + "");
            dto.setCode(item.get("deptCode") + "");
            dto.setParentCode(item.get("parentDeptCode") + "");
            dto.setCampusCode(item.get("campusId") + "");
            dto.setCampusName(item.get("campus") + "");
            dto.setSpell(item.get("spell") + "");
            dtoList.add(dto);
        });
        return ResultDto.data(dtoList);
    }

    @Override
    public ResultDto add(DepartmentVo vo) {
        return ResultDto.data(departmentMapper.insert(JSONCopy.copy(vo, DepartmentEntity.class)));
    }

    @Override
    public ResultDto update(DepartmentVo vo) {
        return ResultDto.data(departmentMapper.updateById(JSONCopy.copy(vo, DepartmentEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(departmentMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(departmentMapper.status(id));
    }


    @Override
    public ResultDto fresh() {
        DepartmentEntity departmentEntity = new DepartmentEntity();
        departmentEntity.setDeptType("0");
        JSONObject jsonObject = HttpTool.postJson("opt_0002", departmentEntity);
        int cSize = 0;
        int dSize = 0;
        if (jsonObject.get("result") != null) {
            List<HttpDepartmentDto> dtoList = JSONArray.parseArray(jsonObject.get("result").toString(), HttpDepartmentDto.class);
//            redisTemplate.opsForValue().set("TaskGetDepartment", JSONObject.toJSONString(dtoList));
//                      需要将数据存入数据库
            cSize = saveCampus(dtoList);
            dSize = saveDepartment(dtoList);
        }
        return ResultDto.data("刷新数据成功,共 " + cSize + " 个院区;共 " + dSize + " 个科室");
    }

    private int saveCampus(List<HttpDepartmentDto> list) {

        Map<String, HttpDepartmentDto> mapCampus = new TreeMap<>();
        for (HttpDepartmentDto dto : list
        ) {
            mapCampus.put(dto.getCampusId(), dto);
        }
//        保存院区
        mapCampus.forEach((k, v) -> {
            CampusEntity entity = new CampusEntity();
            entity.setCode(k);
            List<CampusDto> campusDtoList = campusMapper.select(entity);
            entity.setName(v.getCampus());
            if (campusDtoList.size() == 0) {
                campusMapper.insert(entity);
            } else {
                entity.setId(campusDtoList.get(0).getId());
                campusMapper.updateById(entity);
            }
        });
        return mapCampus.size();
    }


    private int saveDepartment(List<HttpDepartmentDto> list) {
        list.forEach(item -> {
            DepartmentEntity entity = new DepartmentEntity();
            entity.setCode(item.getDeptCode());
            List<DepartmentDto> dtoList = departmentMapper.select(entity);
            entity.setCampusCode(item.getCampusId());
            entity.setParentCode(item.getParentDeptCode());
            entity.setName(item.getDeptName());
            entity.setStatus(1);
            entity.setAddress(item.getVisitAddress());
            entity.setDepartmentType(item.getDeptType());
            entity.setHasChildDepartment(item.getHasChildDept());
            entity.setDepartmentLevel(item.getDeptLevel());
            entity.setCampusName(item.getCampus());
            entity.setParentName(item.getDeptDesc());
            entity.setSpell(item.getSpell());
            if (dtoList.size() == 0) {
                departmentMapper.insert(entity);
            } else {
                entity.setId(dtoList.get(0).getId());
                departmentMapper.updateById(entity);
            }
        });
        return list.size();
    }
}
